package com.chhuang.ewcrm.bean;

import com.chhuang.ewcrm.string.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @author chhuang
 * @create 2020-10-25-8:48
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class QueryBean<T> {
    private int draw=0;
    private String orderBy;
    private int pageSize=50;//每页几行
    private int pageNum=1;//第几页
    private int pages=0;//总页数
    private int limit=pageSize;//每页几行
    private int offset=0;//起始记录
    private long total=0l;//总记录数
    private String search;//全属性模糊查询
    private T query;

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
        this.offset = (pageNum-1)*this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        this.limit = pageSize;
        this.offset = (this.pageNum-1)*pageSize;
    }

    public void setOffset(int offset) {
        this.offset = offset;
        this.pageNum = (offset/this.limit)+1;
    }

    public void setLimit(int limit) {
        this.limit = limit;
        this.pageSize = limit;
        this.pageNum = (this.offset/limit)+1;
    }

    /**
     * 设置排序字段，自动把驼峰式名称改为下划线式数据库用名称
     * @param orderBy
     */
    public void setOrderBy(String orderBy) {
        if(StringUtils.isNotEmpty(orderBy)){
            String[] strs = orderBy.split(",");
            String temp = "";
            for(String s : strs){
                String[] ss= s.split(" ");
                String tmp = StringUtil.humpToLine(ss[0]) + (ss.length==1?"":" "+ss[1]);
                temp += tmp+",";
            }
            this.orderBy = temp.substring(0, temp.length()-1);
        }
    }
}
